package assetsmanagement

import (
	"errors"
	"k8s-admin/dao/assetsmanagement"
	"k8s-admin/global"
	"k8s-admin/models"
	"k8s-admin/utils"

	"github.com/shirou/gopsutil/cpu"
)

type AssetsManagementServiceInterface interface {
	Create(host, username, password, assets_name, assets_class string, port, status *int) error
	List(limit, page int) (*models.AssetsManagementList, error)
	Delete(assets_uuid string) error
	Update(assets_uuid, host, username, password, assets_name, assets_class string, port, status *int) error
	GetLocalHostCpuInfo() ([]utils.CPUInfo, error)
	// usage_rate
	GETLocalHostCpuUsageRate() (string, error)
	GETLocalHostCpuUsageRateDetail() ([]cpu.TimesStat, error)
	GETHOSTCPUInfo(assets_uuid string) ([]models.AssetsManagementInfo, error)
	// 统一展示数据获取接口
	AssetsManagementInfoAll() ([]map[string]interface{}, error)
}
type AssetsManagementService struct{}

func NewAssetsManagementService() AssetsManagementServiceInterface {
	return &AssetsManagementService{}
}
func (a *AssetsManagementService) Create(host, username, password, assets_name, assets_class string, port, status *int) error {
	if err := assetsmanagement.NewAssetsManagement().Create(host, username, password, assets_name, assets_class, port, status); err != nil {
		global.TPLogger.Error(err)
		return errors.New("资源数据添加失败，请重新添加")
	} else {
		return nil
	}

}
func (a *AssetsManagementService) List(limit, page int) (*models.AssetsManagementList, error) {
	list, err := assetsmanagement.NewAssetsManagement().List(limit, page)
	if err != nil {
		global.TPLogger.Error(err)
		return nil, errors.New("资源数据查询失败，请稍后重试")
	} else {
		return list, nil
	}
}

func (a *AssetsManagementService) Delete(assets_uuid string) error {
	if err := assetsmanagement.NewAssetsManagement().Delete(assets_uuid); err != nil {
		global.TPLogger.Error(err)
		return errors.New("资源数据删除失败，请稍后重试")
	} else {
		return nil
	}
}

func (a *AssetsManagementService) Update(assets_uuid, host, username, password, assets_name, assets_class string, port, status *int) error {
	err := assetsmanagement.NewAssetsManagement().Update(assets_uuid, host, username, password, assets_name, assets_class, port, status)
	if err != nil {
		global.TPLogger.Error(err)
		return errors.New("资源数据更新失败，请稍后重试")
	} else {
		return nil
	}
}

func (a *AssetsManagementService) GetLocalHostCpuInfo() ([]utils.CPUInfo, error) {
	cpuListInfo, err := utils.CpuInfo()
	if err != nil {
		global.TPLogger.Error(err)
		return nil, errors.New("获取CPU信息失败，请稍后重试")
	} else {
		return cpuListInfo, nil
	}
}

func (a *AssetsManagementService) GETLocalHostCpuUsageRate() (string, error) {
	usage_rate, err := utils.CpuPercent()
	if err != nil {
		global.TPLogger.Error(err)
		return "", errors.New("获取CPU信息失败，请稍后重试")
	} else {
		return usage_rate, nil
	}
}

// 数据展示
func (a *AssetsManagementService) GETLocalHostCpuUsageRateDetail() ([]cpu.TimesStat, error) {
	usage_rate_detail, err := utils.CPUTimes()
	if err != nil {
		global.TPLogger.Error(err)
		return nil, errors.New("获取CPU信息失败，请稍后重试")
	} else {
		return usage_rate_detail, nil
	}
}

// 用于资源监控
func (a *AssetsManagementService) GETHOSTCPUInfo(assets_uuid string) ([]models.AssetsManagementInfo, error) {
	cpuListInfo, err := assetsmanagement.NewAssetsManagement().GETHOSTCPUInfo(assets_uuid)
	if err != nil {
		global.TPLogger.Error(err)
		return nil, errors.New("获取CPU信息失败，请稍后重试")
	} else {
		return cpuListInfo, nil
	}
}

// 统一展示数据获取接口
func (a *AssetsManagementService) AssetsManagementInfoAll() ([]map[string]interface{}, error) {
	cpu_info, err := utils.CpuInfo()
	cpu_percent_info, err := utils.CpuPercent()
	host_info, err := utils.HostInfo()
	meniry_info, err := utils.MeniryInfo()
	disk_info, err := utils.DiskInfo()
	if err != nil {
		global.TPLogger.Error(err)
		return nil, errors.New("获取资产信息失败，请稍后重试")
	} else {
		return []map[string]interface{}{
			{"cpu_info": cpu_info},
			{"cpu_percent_info": cpu_percent_info},
			{"host_info": host_info},
			{"meniry_info": meniry_info},
			{"disk_info": disk_info},
		}, nil
	}
}
